package castledefense.logic.effects;

import java.util.ArrayList;

import castledefense.data.GameData;
import castledefense.game.render.RenderSpellEffect;
import castledefense.game.screens.MainGameScreen;
import castledefense.logic.Map;
import castledefense.logic.units.Unit;
import castledefense.sound.SoundHandler;

public final class ActivatedAbilities {
	final Map map;
	final RenderSpellEffect effectsHandler;
	final int[] coolDown = new int[4];
	
	public ActivatedAbilities(Map map, RenderSpellEffect effectsHandler) {
		this.map = map;
		this.effectsHandler = effectsHandler; 
		//normal cd - 60s for merrier start
		for (int i = 0; i < 4; i++) {
			coolDown[i] = GameData.getCd(i) - 60*1000;
		}

	}

	public float coolDownLeftAsFloat(int type){
		return 1f-(float)coolDown[type] / (float)GameData.getCd(type);
	}
	
	public void update(int delta) {
		for (int i = 0; i < 4; i++) {
			coolDown[i] = coolDown[i] > 0 ? coolDown[i] - delta : 0;
		}
	}

	/** true if coolDown==0, false otherwise */
	public boolean activated(int type) {
		if (type < 0 || type >= 4) {
			return false;
		}
		if (coolDown[type] > 0) {
			return false;
		}
		final ArrayList<Unit> tmp = map.getUnitList();
		final int size = tmp.size();
		if (type == GameData.fire) {
			// pyromancer
			SoundHandler.pyromancerSound();
			for (int i=0;i<size;i++){
				Unit unit = tmp.get(i);				
				unit.applyDot(10000, 1000,unit.getInitialHealth()/50 ,GameData.fire );
			}
			
			effectsHandler.addPyromancer();
			map.getCastle().damage(map.getCastle().getInitialLife() / 5);
			coolDown[type] = GameData.getCd(type);
		} else if (type == GameData.water) {
			// flood
			SoundHandler.floodSound();
			for (int i=0;i<size;i++){
				Unit unit = tmp.get(i);
				//unit.decreaseHealth(unit.getInitialHealth() / 10, GameData.noElement);
				unit.slow(20000, 0.5);
			}
			effectsHandler.addFlood();
			coolDown[type] = GameData.getCd(type);
		} else if (type == GameData.earth) {
			// earthQuake
			SoundHandler.quakeSound();
			for (int i=0;i<size;i++){
				Unit unit = tmp.get(i);
				//unit.decreaseHealth(unit.getInitialHealth() / 5, GameData.noElement);
				unit.stun(5000);
			}
			effectsHandler.addQuake();
			coolDown[type] = GameData.getCd(type);
		} else if (type == GameData.air) {
			// blessed wind
			SoundHandler.blessedWindSound();
			MainGameScreen.missileSystem.addBonusDamage(10000, 1.5);
			effectsHandler.addBlessedWind();
			coolDown[type] = GameData.getCd(type);
		}

		return true;
	}

}
